import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.StringTokenizer;


public class JoostTest extends Setup{
	static String FILE_PATH_GENRE="../track1/genreData1.txt";
	static String FILE_PATH_ARTIST="../track1/artistData1.txt";
	static String FILE_PATH_ALBUM="../track1/albumData1.txt";
	static String FILE_PATH_TRACK="../track1/trackData1.txt";
	
	public static void main(String args[]){
		setup();
		try {
			readGenre();
		
			readArtist();
			readAlbum();
			readTrack();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static void readGenre() throws Exception {
		File file = new File(FILE_PATH_GENRE);
		BufferedReader bufRdr = new BufferedReader(new FileReader(file));
		String line = null;
		int genreID = -1;		

		while ((line = bufRdr.readLine()) != null) {
			genreID = Integer.valueOf(line);
			ITEMA[genreID] = new Genre(ITEMA[genreID]);
		}
		bufRdr.close();
	}
	
	private static void readArtist() throws Exception {
		File file = new File(FILE_PATH_ARTIST);
		BufferedReader bufRdr = new BufferedReader(new FileReader(file));
		String line = null;
		int artistID = -1;		

		while ((line = bufRdr.readLine()) != null) {
			artistID = Integer.valueOf(line);
			ITEMA[artistID] = new Artist(ITEMA[artistID]);
		}
		bufRdr.close();
	}
	
	private static void readAlbum() throws Exception {
		File file = new File(FILE_PATH_ALBUM);
		BufferedReader bufRdr = new BufferedReader(new FileReader(file));
		String line = null;
		StringTokenizer st;
		int albumID = -1;
		int artistID = -1;
		Album album;

		while ((line = bufRdr.readLine()) != null) {
			st = new StringTokenizer(line, "|");
			albumID = Integer.valueOf(st.nextToken());
			artistID = parse(st.nextToken());
			album = new Album(ITEMA[albumID]);
			album.artist = (Artist)ITEMA[artistID];
			ITEMA[albumID] = album;
		}
		bufRdr.close();
	}
	
	private static void readTrack() throws Exception {
		File file = new File(FILE_PATH_GENRE);
		BufferedReader bufRdr = new BufferedReader(new FileReader(file));
		String line = null;
		StringTokenizer st;
		int trackID = -1;
		int albumID = -1;
		int artistID = -1;
		
		Track track;


		while ((line = bufRdr.readLine()) != null) {
			st = new StringTokenizer(line, "|");
			trackID = Integer.valueOf(st.nextToken());
			albumID = parse(st.nextToken());
			artistID = parse(st.nextToken());
			track = new Track(ITEMA[trackID]);
			track.album = (Album)ITEMA[albumID];
			track.artist = (Artist)ITEMA[artistID];
			ITEMA[trackID] = track;
		}
		bufRdr.close();
	}
	
	private static int parse(String str){
		if(str.equals("None")){
			return -1;
		} else {
			return Integer.parseInt(str);
		}
	}
}
